<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      遍历一条边，其实也是用回溯，只不过每次都有返回值，只要找到一条边满足条件，就直接return

      */
      var hasPathSum = function (root, targetSum) {
        if (!root) return
        function backTracking(root, sum) {
          if (!root.left && !root.right && sum == targetSum) {
            return true
          }
          if (!root.left && !root.right) {
            return false
          }
          if (root.left) {
            if (backTracking(root.left, sum + root.left.val)) return true
          }
          if (root.right) {
            if (backTracking(root.right, sum + root.right.val)) return true
          }
          //没找到
          return false
        }
        return backTracking(root, 0)
      }
    </script>
  </body>
</html>
